package cc.linker.steplink.crm.merchant.service;

import cc.linker.steplink.crm.commons.service.CommonsService;
import cc.linker.steplink.crm.commons.utils.NumberUtils;
import cc.linker.steplink.crm.merchant.dao.AchievementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Service
public class AchievementService {

    @Autowired
    private AchievementMapper achievementMapper;

    @Autowired
    private CommonsService commonsService;

    public List<Map<String,Object>> list(Map<String, Object> param) {
        return achievementMapper.list(param);
    }

    public void exportList(Map<String, Object> param, HttpServletResponse response) throws Exception {
        //
        String[] title = {
                "时间", "代理名称", "代理手机号", "返润用户", "返润用户手机号",
                "是否下级","返润类型","返润金额","结算状态","订单号","商品名称"};
        String fileName = "";
        if(StringUtils.isEmpty(param.get("startTime")) || StringUtils.isEmpty(param.get("endTime"))){
            fileName = "返润明细";
        }else{
            fileName = param.get("startTime").toString()+"-"+param.get("endTime").toString()+"返润明细";
        }
        String[] strings = {"createTime", "receiveName", "receivePhone", "payName", "payPhone",
                "ifParent","type","amount","status","orderSn","orderName"};
        List<Map<String, Object>> list = achievementMapper.list(param);

        for (Map<String, Object> stringObjectMap : list) {
            stringObjectMap.put("createTime",stringObjectMap.get("createTime").toString().substring(0,19));
            stringObjectMap.put("ifParent", "1".equals(stringObjectMap.get("ifParent").toString())?"是":"否");
            String type;
            switch (stringObjectMap.get("type").toString()) {
                case "2":
                    type = "商品服务费";
                    break;
                case "3":
                    type = "商品推荐费";
                    break;
                case "16":
                    type = "创作奖励";
                    break;
                default:
                    type = "";
            }
            stringObjectMap.put("type", type);
            String status = "结算中";
            if("1".equals(stringObjectMap.get("status").toString())){
                status = "结算完成";
            }else if("2".equals(stringObjectMap.get("status").toString())){
                status = "结算关闭";
            }else if("3".equals(stringObjectMap.get("status").toString())){
                status = "退款中";
            }else if("4".equals(stringObjectMap.get("status").toString())){
                status = "已退款";
            }
            stringObjectMap.put("status", status);
        }
        commonsService.excelDownload(list, strings, title, fileName, response);
    }

    public List<Map<String,Object>> report(Map<String, Object> param) {
        return achievementMapper.report(param);
    }

    public void exportReport(Map<String, Object> param, HttpServletResponse response) throws Exception{
        String[] title = {
                "代理名称", "代理手机号", "发展下级", "推广订单数", "返润金额",
                "服务费","推荐费","下级交易额","下级代理交易额","创作奖励"};
        String fileName = param.get("startTime").toString()+"-"+param.get("endTime").toString()+"业绩报表";
        String[] strings = {"name", "phone", "sonCount", "orderCount", "allRebate",
                "serviceFee","recommendFee","sonDealFee","promoterDealFee","authorFee"};
        List<Map<String, Object>> list = achievementMapper.report(param);
        commonsService.excelDownload(list, strings, title, fileName, response);
    }
}
